package com.lagou.boot;

import com.lagou.client.RPCConsumer;
import com.lagou.common.GlobalStatic;
import com.lagou.service.IUserService;
import com.lagou.watch.MyCuratorWatcher;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

import java.util.List;


public class ConsumerBoot {

    //参数定义
    private static final String PROVIDER_NAME = "UserService#sayHello";

    public static void main(String[] args) throws Exception {
        //获取服务端列表
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("192.168.31.227:2181",
                5000,1000,retryPolicy);
        client.start();
        List<String> childes = client.getChildren().forPath("/provide");
        for(String child:childes){
            String data = new String(client.getData().forPath("/provide/"+child));
            GlobalStatic.servers.put(child,data);
        }
        new Thread(new MyCuratorWatcher(client)).start();
        //1.创建代理对象
        IUserService service = (IUserService) RPCConsumer.createProxy(IUserService.class, PROVIDER_NAME,client);
        //2.循环给服务器写数据
        while (true) {
            String result = service.sayHello("are you ok !!");
            System.out.println(result);
            Thread.sleep(2000);
        }
    }
}
